package cn.com.callback

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import cn.com.elite.Util
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONObject

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: api_SmsSend* @Description:华盛证券 短信发送 --内部接口
 * @author: xpf* @date 2021-07-07 10:28:00
 *
 */

public class api_Consultant_monitoring_data implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(api_Consultant_monitoring_data.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);

        String inData = "";
        String outData = "";
        String outcount;
        String dataSourceID = "dataSource";
        String msg = "";

        BufferedReader iny = null;
        if (getMethod.equals("GET")) {
            inData = request.getParameter("data");
            log.debug("原始Get请求参数:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("原始Post请求参数:" + inData);
        }

        try {
            log.debug("api_Consultant_monitoring_data开始获取大屏数据--------------------------");
            def getSmsSelectSQL = eliteBean.chaxun_sql("SELECT HANDLEBY_ID as HANDLEBY_ID,count(*) sen_num,sum( CASE WHEN MODIFIEDDATE IS NOT NULL THEN 1 ELSE 0 END ) han_num, sum( CASE WHEN OBJECTIVESUBSTATUS = 'FA0000' THEN 1 ELSE 0 END ) FA0000_num,sum( CASE WHEN OBJECTIVESUBSTATUS = 'NOTCON' THEN 1 ELSE 0 END ) no_call_num, sum( CASE WHEN OBJECTIVESUBSTATUS = 'OPEN' THEN 1 ELSE 0 END ) open_num,\tsum( CASE WHEN OBJECTIVESUBSTATUS = 'SU0000' THEN 1 ELSE 0 END ) ok_num   FROM objective where 1=1  AND  CREATEDDATE >= DATE_SUB( curdate(), INTERVAL 0 DAY ) AND MAP_ID = 'MY48ZX' GROUP BY HANDLEBY_ID", "dataSource");
            log.debug("SQL获取数据:" + getSmsSelectSQL);

            String HANDLEBY_ID = "";
            String sen_num = "";
            String han_num = "";
            String FA0000_num = "";
            String no_call_num = "";
            String open_num = "";
            String ok_num = "";

            if (getSmsSelectSQL.size() > 0) {
				for (int i = 0; i < getSmsSelectSQL.size(); i++) {
                log.debug("api_Consultant_monitoring_data开始处理时间--------------------------")
                HANDLEBY_ID = getSmsSelectSQL.get(i).get("HANDLEBY_ID");
                sen_num = getSmsSelectSQL.get(i).get("sen_num");
                han_num = getSmsSelectSQL.get(i).get("han_num");
                FA0000_num = getSmsSelectSQL.get(i).get("FA0000_num");
                no_call_num = getSmsSelectSQL.get(i).get("no_call_num");
                open_num = getSmsSelectSQL.get(i).get("open_num");
                ok_num = getSmsSelectSQL.get(i).get("ok_num");
				log.debug("处理人：" + HANDLEBY_ID + ",下发数：" + sen_num + ",处理数:" + han_num + ",无需求:" + FA0000_num + ",无法联系:" + no_call_num + ",跟进中:" + open_num + ",营销成功:" + ok_num);

				def getADSSelectSQL = eliteBean.chaxun_sql("select createddate from agent_adviser_state  where createddate= date_format(now(), '%Y-%m-%d') and agent_id='"+HANDLEBY_ID+"'", "dataSource");
                if (getADSSelectSQL.size() > 0) {
					def updatesamp = eliteBean.update("UPDATE  agent_adviser_state SET   Distribution_num = '"+sen_num+"', handle_num = '"+han_num+"', no_demand_num = '"+FA0000_num+"', no_COMMENT_num = '"+no_call_num+"', Follow_up = '"+open_num+"', Marketing_success = '"+ok_num+"', MODIFIEDDATE = NOW() WHERE createddate =date_format(now(), '%Y-%m-%d') and agent_id='"+HANDLEBY_ID+"'", "dataSource");
							log.debug("进入了update");
                } else {
                    //接口入参

                        def insertsamp = eliteBean.update("insert into agent_adviser_state (createddate,agent_id,Distribution_num,handle_num,no_demand_num,no_COMMENT_num,Follow_up,Marketing_success) " +
                                "values(now(),'" + HANDLEBY_ID + "','" + sen_num + "','" + han_num + "','" + FA0000_num + "','" + no_call_num + "','" + open_num + "','" + ok_num + "')", "dataSource");
						log.debug("进入了insert");
                    }
                }
            } else {
                log.debug("api_Consultant_monitoring_data异常--------------------------");
            }

            outData = "{\"code\":\"1\",\"message\":\"成功\",\"data\":{\"custtype\":\"" + 1 + "\"}}";
            //log.debug("返回Json:" + reJoson);

        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            outData = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + ")\",\"data\":[]}";
        }
        log.debug("处理完成返回结果:" + outData);
        return outData;
    }

    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {
                        Rev_Value = "";
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        log.debug("解析-k=" + getKey + " v=" + Rev_Value);
        return Rev_Value;
    }


    // 非空校验isNull
    public static boolean isNull(String value) {

        boolean reFlag = true;
        if (value == "" || value.length() <= 0) {
            reFlag = true;
        } else {
            reFlag = false;
        }
        log.debug(reFlag);
        return reFlag;
    }

    // 非空校验isNullMSG
    public static String isNullMSG(String key, String value) {

        String reResult = "";
        if (value == "" || value.length() <= 0) {
            reResult = key + "必填字段;";
        }
        return reResult;
    }

}
